package com.czk.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.czk.springcloud.entity.CommonResult;
import com.czk.springcloud.entity.Payment;
import com.czk.springcloud.fallBackClass.BlockHandlerClass;
import com.czk.springcloud.fallBackClass.FallBackClass;
import com.czk.springcloud.ownFeign.register.MyFeignService;
import com.czk.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/12/26 20:32
 */
@RestController
@Slf4j
@RequestMapping("/consumer")
public class CircleBreakerController {
    public static final String SERVICE_URL = "http://nacos-payment-provider";
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/fallback/{id}")
    // 此时没有兜底方法
    // @SentinelResource(value = "fallback")
    // you兜底方法，发生异常走handlerFallback方法
    // @SentinelResource(value = "fallback", fallback = "handlerFallback")
    // @SentinelResource(value = "fallback", blockHandler = "blockHandler")
    @SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler")
    public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
        CommonResult commonResult = restTemplate.getForObject(SERVICE_URL + "/payment/sql/" + id, CommonResult.class, id);
        if (id == 4) {
            throw new IllegalArgumentException("非法参数异常");

        } else if (commonResult.getData() == null) {
            throw new NullPointerException("该id没有记录，非法参数异常");
        }
        return commonResult;
    }

    /**
     * 异常兜底方法
     * 注意方法参数应该除了异常其余应该与上面一致，不然无法完成服务降级
     *
     * @param id
     * @param throwable
     * @return
     */
    public CommonResult handlerFallback(@PathVariable("id") Long id, Throwable throwable) {
        Payment payment = new Payment(id, "null");
        return new CommonResult(444, "此方法是兜底异常方法 " + throwable.getMessage(), payment);
    }

    /**
     * 此时是服务限流方法
     *
     * @param id
     * @param blockException
     * @return
     */
    public CommonResult blockHandler(@PathVariable Long id, BlockException blockException) {
        Payment payment = new Payment(id, "null");
        return new CommonResult(445, "blockHandler-sentinel 限流方法" + blockException.getMessage(), payment);
    }

    /**
     * 注入feign客户端
     */
    @Resource
    private PaymentService paymentService;

    /**
     * feign客户端的测试
     *
     * @param id
     * @return
     */
    @GetMapping("/paymentSql/{id}")
    @SentinelResource(value = "paymentSql",fallbackClass = FallBackClass.class,fallback = "handlerException2"

    ,blockHandlerClass = BlockHandlerClass.class,blockHandler = "handlerException")
    public CommonResult<Payment> paymentSql(@PathVariable("id") Long id) {
        if (id>3){
            throw new RuntimeException("参数错误");
        }
        return paymentService.paymentSql(id);
    }
    @Autowired
    private MyFeignService myFeignService;
    @GetMapping("/myfeign/{id}")
    public CommonResult<Payment> paymentSql2(@PathVariable("id") Long id) {
        if (id>3){
            throw new RuntimeException("参数错误");
        }
        return myFeignService.paymentSql(id);
    }
}
